define(['jquery','api','user','util','text!pages/bank/channel.html','Vue'],function($,api,user,util,tpl,Vue){
	var vm,
	   banks={},//银行列表
       channels={},//通道列表
       userchannels={},
       userchannelsTimer,
       banksTimer,
       channelsTimer,
	   lang={
	      0:'我要提现',
	      2:'微信授权',
	      1:'去绑银行卡'
	   },
	   controller = {
	   init:function(backUrl){
	   	   if(!user.checkLogin())return;
	   	   	api.headerShow(1);
			api.headerTitle(L('银行通道'));
	   	   	api.headerLeftLink('javascript:history.go(-1)');
	   	   api.footerShow(0);//隐藏底部导航
		   api.setTitle(L('银行通道'));
		   
		   this.render();
	   },
	   render:function(){
	   	var that=this;
	   		$('#common').html('<div id="channel"></div>');
			vm=new Vue({
           		el:'#channel',
	           data:{
	           	  show:true,
	           	  loading:false,
	           	  outChannel:'',
	           	  channels:[],
	           	  buttonValue:'下一步',
	           	  banks:[]
	           },
	           methods:{
	              bind:function(){
	              	var that=this;
            		this.buttonValue = '跳转中…';
       	    		setTimeout(function(){
       	    			location.hash='#/fund/bind/'+that.outChannel+'/'+encodeURIComponent(location.href);
       	    		},500);
	              }
	           },
	           template:tpl
        	});
			
			vm.loading=true;
	   		that.getUserInfo();
	   },
	   initChannel:function(){
	   		vm.loading=false;
	   	   var 
	   	       channelHash={},
	   	       channel,
	   	       channelid,
	   	       userchannel,
	   	       temp=[],
	   	       cb=function(){
	   	       	  for(var i=0;i<userchannels.length;i++){
	   	       	  	   channel=userchannels[i];
	   	       	  	if (channel.isprimary==='49'){
		   	       	       channelHash[channel.channelid]=channel;
	   	       	  	   }
	   	       	  }

	   	       	  for(var i=0;i<channels.length;i++){
	   	       	  	   channel=channels[i];
	   	       	       if(+channel.outmode>50 || channel.channelid==48){
		   	       	  	  channelid=channel.channelid;
		   	       	  	  userchannel=channelHash[channelid];
		   	       	  	  // 用户通道中没有银行id的通道才是没绑定的通道
		   	       	  	  if(userchannel && userchannel.bankid===''){
		   	       	  	  	  channel["bankid"]=userchannel.bankid;
	                      	  channel.bankaccount=userchannel.bankaccount;
	                      	  channel.bank=userchannel.bankid&&banks[userchannel.bankid] ? banks[userchannel.bankid] : '';
		   	       	       	  temp.push(channel);
		   	       	  	  }
	   	       	       }
	   	       	  }
	   	       	  
	           	  vm.channels=temp;
	   	      };
	       
	       if(banks && userchannels && channels)cb();
	   },
	   getUserInfo:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getUserInfo();
	   			};
	   			
   			api.userInfo(
   				function(rs){
			   	   if(rs.retcode=='0'){
			   	   		vm.userfunds=rs.data;
			   	   		userchannels=rs.data.channel?rs.data.channel:[];
			   	   		that.getBanks();
			   	   }else{
			   	      util.msg('用户未登录或超时！');
			   	      userchannelsTimer = setTimeout(cb,2000);
			   	   }
			   },
			   function(){//err
			   		userchannelsTimer = setTimeout(cb,2000);
			   }
			);
	   },
	   getBanks:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getBanks();
	   			};
	   		// 获取银行列表
       	  	api.banks(
       	  		function(rs){
				   if(rs.retcode=='0' && rs.data){
					banks = rs.data;
					that.getChannels();
				   }else if(rs.retcode=='43'){
				       util.msg('系统缓存服务bug,请联系管理员！')
				       banksTimer = setTimeout(cb,2000);
				   }else{
				   		banksTimer = setTimeout(cb,2000);
				   }
				},
				function(){//err
					banksTimer = setTimeout(cb,2000);
				}
   	  		);
	   },
	   getChannels:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getChannels();
	   			};
	   			
	   		api.channels(
	   			function(rs){
		           if(rs.retcode=='0' && rs.data){
		           	  channels=rs.data;
		           	  that.initChannel();
		           }else{
		           		channelsTimer = setTimeout(cb,2000);
		           }
		       },
		       function(){//err
		       		channelsTimer = setTimeout(cb,2000);
		       }
   			);
	   },
	   cleartFullTimer:function(){
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(banksTimer) clearTimeout(banksTimer);
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(userchannelsTimer) clearTimeout(userchannelsTimer);
	   },
	   onRouteChange:function(){
	   	if(vm)vm.show=false;
	   	this.cleartFullTimer();
	   }
	};
    return controller;
})